home *** CD-ROM | disk | FTP | other *** search
/ The Original Shareware 1.1 / The Original Shareware (WeMake CDs)(Volume 1.1)(CDs, Inc)(1993).iso / 6 / c_math.zip / ATAN.C < prev    next >
Text File  |  1983-07-02  |  2KB  |  111 lines

  1. /*
  2.     floating-point arctangent
  3.  
  4.     atan returns the value of the arctangent of its
  5.     argument in the range [-pi/2,pi/2].
  6.  
  7.     atan2 returns the arctangent of arg1/arg2
  8.     in the range [-pi,pi].
  9.  
  10.     there are no error returns.
  11.  
  12.     coefficients are #5077 from Hart & Cheney. (19.56D)
  13. */
  14.  
  15.  
  16. double static sq2p1     =2.414213562373095048802e0;
  17. static double sq2m1     = .414213562373095048802e0;
  18. static double pio2     =1.570796326794896619231e0;
  19. static double pio4     = .785398163397448309615e0;
  20. static double p4     = .161536412982230228262e2;
  21. static double p3     = .26842548195503973794141e3;
  22. static double p2     = .11530293515404850115428136e4;
  23. static double p1     = .178040631643319697105464587e4;
  24. static double p0     = .89678597403663861959987488e3;
  25. static double q4     = .5895697050844462222791e2;
  26. static double q3     = .536265374031215315104235e3;
  27. static double q2     = .16667838148816337184521798e4;
  28. static double q1     = .207933497444540981287275926e4;
  29. static double q0     = .89678597403663861962481162e3;
  30.  
  31.  
  32. /*
  33.     atan makes its argument positive and
  34.     calls the inner routine satan.
  35. */
  36.  
  37. double
  38. atan(arg)
  39. double arg;
  40. {
  41.     double satan();
  42.  
  43.     if(arg>0)
  44.         return(satan(arg));
  45.     else
  46.         return(-satan(-arg));
  47. }
  48.  
  49.  
  50. /*
  51.     atan2 discovers what quadrant the angle
  52.     is in and calls atan.
  53. */
  54.  
  55. double
  56. atan2(arg1,arg2)
  57. double arg1,arg2;
  58. {
  59.     double satan();
  60.  
  61.     if((arg1+arg2)==arg1)
  62.         if(arg1 >= 0.) return(pio2);
  63.         else return(-pio2);
  64.     else if(arg2 <0.)
  65.         if(arg1 >= 0.)
  66.             return(pio2+pio2 - satan(-arg1/arg2));
  67.         else
  68.             return(-pio2-pio2 + satan(arg1/arg2));
  69.     else if(arg1>0)
  70.         return(satan(arg1/arg2));
  71.     else
  72.         return(-satan(-arg1/arg2));
  73. }
  74.  
  75. /*
  76.     satan reduces its argument (known to be positive)
  77.     to the range [0,0.414...] and calls xatan.
  78. */
  79.  
  80. static double
  81. satan(arg)
  82. double arg;
  83. {
  84.     double    xatan();
  85.  
  86.     if(arg < sq2m1)
  87.         return(xatan(arg));
  88.     else if(arg > sq2p1)
  89.         return(pio2 - xatan(1.0/arg));
  90.     else
  91.         return(pio4 + xatan((arg-1.0)/(arg+1.0)));
  92. }
  93.  
  94. /*
  95.     xatan evaluates a series valid in the
  96.     range [-0.414...,+0.414...].
  97. */
  98.  
  99. static double
  100. xatan(arg)
  101. double arg;
  102. {
  103.     double argsq;
  104.     double value;
  105.  
  106.     argsq = arg*arg;
  107.     value = ((((p4*argsq + p3)*argsq + p2)*argsq + p1)*argsq + p0);
  108.     value = value/(((((argsq + q4)*argsq + q3)*argsq + q2)*argsq + q1)*argsq + q0);
  109.     return(value*arg);
  110. }
  111.